import os
import asyncio
from pyppeteer import launch

from jiuyun.settings import BASE_DIR

def save_image_async(html_file_path, img_path):
    async def save_image(url,img_path):
        """
        单据生成图片
        :param url:
        :param img_path:
        :return:
        """
        brower = await launch()
        page = await brower.newPage()
        # 加载指定网页
        await page.goto(url)
        print('--->', await page.title())
        # 设置网页显示尺寸
        await page.setViewport({'width':1920,'height':1080})
        print('---->', img_path)
        await page.screenshot({'path':img_path,'clip':{'x':0,'y':0,'width':1500,'height':780}})
        await brower.close()
        print('save OK')

    loop = asyncio.get_event_loop()
    loop.run_until_complete(save_image(r"file://"+
                                       html_file_path, img_path))
    return True

if __name__ == '__main__':
    from xmlrpc.server import SimpleXMLRPCServer
    s = SimpleXMLRPCServer(('localhost', 9800))
    s.register_function(save_image_async, 'save_image')
    print('run rpc Server')
    s.serve_forever()






